/*文件注释
 * ===============================================================================
 * 说明：
 * 作者：zengyy
 * 版本：v1.0
 * 创建日期：2013-05-04 10:20:10
 * 完成日期： 
 * 修改纪录：  
 * ===============================================================================
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace SRF.Admin
{
    /// <summary>
    /// 用户 Service
    /// </summary>
    public class UserService
    {
        readonly Data.DBHelperBase dbhelper = Data.DBHelperFactory.GetDBHelper();

        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="id">用户id</param>
        /// <returns>返回用户</returns>
        public Models.User Get(string id)
        {
            return dbhelper.GetQuery("admin.user.get").SetParameter(new { Id = id }).UniqueResult<Models.User>();
        }
        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="id">用户id</param>
        /// <returns>返回用户</returns>
        public string GetRoles(string id)
        {
            string str = "";
            IList<string> roles = dbhelper.GetQuery("admin.user.getroles").SetParameter(new { UserId = id }).List<string>();
            foreach (string role in roles)
            {
                str += role + ",";
            }
            return str.TrimEnd(',');
        }
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns>返回用户列表</returns>
        public IList<Models.User> List()
        {
            return dbhelper.GetQuery("admin.user.list").List<Models.User>();
        }
        /// <summary>
        /// 保存用户
        /// </summary>
        /// <param name="post">用户</param>
        /// <returns>返回用户Id</returns>
        public string Save(Models.User user)
        {
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required))
            {
                if (string.IsNullOrEmpty(user.Id))
                {
                    user.Id = Guid.NewGuid().ToString();
                    dbhelper.GetQuery("admin.user.insert").SetParameter(user).Execute();
                }
                else
                {
                    dbhelper.GetQuery("admin.user.update").SetParameter(user).Execute();
                }
                dbhelper.GetQuery("admin.user.deleterole").SetParameter(new { UserId = user.Id }).Execute();
                if (!string.IsNullOrEmpty(user.Roles))
                {
                    foreach (string roleid in user.Roles.Split(','))
                    {
                        dbhelper.GetQuery("admin.user.addrole").SetParameter(new { UserId = user.Id, RoleId = roleid }).Execute();
                    }
                }

                transactionScope.Complete();
            }

            return user.Id;
        }

        public void SavePersonal(Models.User user)
        {
            dbhelper.GetQuery("admin.user.savepersonal").SetParameter(user).Execute();
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id">用户Id</param>
        public void Delete(string id)
        {
            dbhelper.GetQuery("admin.user.delete").SetParameter("Id", id).Execute();
        }

        public void Test()
        {
            dbhelper.GetQuery("admin.user.test").Execute();
        }
    }
}